package igou.togogo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import igou.togogo.bean.*;
import igou.togogo.domain.ProductDetail;
import igou.togogo.domain.ProductResult;
import igou.togogo.mapper.EColorMapper;
import igou.togogo.mapper.EImgMapper;
import igou.togogo.mapper.EProductMapper;
import igou.togogo.mapper.ESkuMapper;
import igou.togogo.page.Pagination;
import igou.togogo.pojo.EgouResult;
import igou.togogo.service.IProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    EProductMapper productMapper;

    @Autowired
    EImgMapper eImgMapper;

    @Autowired
    EColorMapper colorMapper;

    @Autowired
    ESkuMapper skuMapper;

    @Override
    public EgouResult getProductByBrandId(Integer brandId, Integer page, Integer rows) {
        EProductExample example = new EProductExample();
        PageHelper.startPage(page, rows);
        if (brandId != -1) {
            example.createCriteria().andBrandIdEqualTo(brandId);
        }
        List<EProduct> list = productMapper.selectByExample(example);
        List<ProductDetail> details = new ArrayList<>();
        for(EProduct product : list) {
            ProductDetail detail = new ProductDetail();
            //复制商品信息
            BeanUtils.copyProperties(product, detail);
            //查询商品图片
            EImgExample imgExample = new EImgExample();
            imgExample.createCriteria().andProductIdEqualTo(product.getId());
            detail.setImgs(eImgMapper.selectByExample(imgExample));
            details.add(detail);
        }
        //返回結果
        PageInfo<EProduct> pageInfo = new PageInfo<>(list);
        Pagination pagination = new Pagination(page, rows, (int) pageInfo.getTotal());
        ProductResult productResult = new ProductResult();
        productResult.setList(details);
        productResult.setPagination(pagination);
        return EgouResult.ok(productResult);
    }

    @Override
    public EgouResult getProductDetail(Integer productId, String colorId) {
        ProductDetail productDetail = new ProductDetail();
        List<EColor> colors = new ArrayList<EColor>();
        //查询商品信息
        EProductWithBLOBs product = productMapper.selectByPrimaryKey(productId);

        if (product == null) {
            return EgouResult.build(400, "没找到数据");
        }

        String strColor = product.getColor();
        String[] colorIds = strColor.split(",");
        for (String c : colorIds) {
            EColor eColor = colorMapper.selectByPrimaryKey(Integer.valueOf(c));
            colors.add(eColor);
        }
        //如果用户没有选择颜色，默认显示第一个颜色
        if (StringUtils.isBlank(colorId)) {
            colorId = colorIds[0];
        }
        //sku
        ESkuExample skuExample = new ESkuExample();
        skuExample.createCriteria().andProductIdEqualTo(productId).andColorIdEqualTo(Integer.parseInt(colorId));
        List<ESku> skus = skuMapper.selectByExample(skuExample);

        BeanUtils.copyProperties(product, productDetail);
        productDetail.setColors(colors);
        productDetail.setSkus(skus);
        return EgouResult.ok(productDetail);
    }

    @Override
    public EgouResult getById(Integer productId) {
        //查询商品信息
        //http://localhost:8082/product/252
        EProductExample example = new EProductExample();
        example.createCriteria().andIdEqualTo(productId);
        List<EProduct> products = productMapper.selectByExample(example);
        return EgouResult.ok(products.get(0));
    }
}
